import axios from 'axios'
import VueRouter from 'vue-router'
// 导入进度条的实现代码以及样式美化
import NProgress from 'nprogress'
// import 'nprogress/nprogress.css'

// 先把VueRouter的原型对象的push方法保存一份
const originPush = VueRouter.prototype.push

// 重写push|replace
// 第一个参数：告诉原来的push方法跳转到哪个路由 ,即原来向push方法传递的参数
VueRouter.prototype.push = function(location, resolve, reject) {
  if (resolve && reject) {
    originPush.call(this, location, resolve, reject)
  } else {
    originPush.call(this, location, () => {}, () => {})
  }
}
// 超时时间
axios.defaults.timeout = 10000
const request = axios.create({
  // 基准路径
  // baseURL: 'http://127.0.0.1:8888/api/private/v1'
  // baseURL: 'http://192.168.101.6:8888/api/private/v1'
  baseURL: 'https://lianghj.top:8888/api/private/v1/'
})
// axios请求拦截
request.interceptors.request.use(config => {
  // 进度条开始
  NProgress.start()
  config.headers.Authorization = localStorage.getItem('token')
  return config
})
// axios响应拦截
request.interceptors.response.use(config => {
  // 进度条结束
  NProgress.done()
  return config
})
export default request
